import config from '../../utils/config'
import {request} from "../../utils/request";
const app = getApp()
Page({
    data: {
        picUrl:config.picUrl,
        custom:wx.getMenuButtonBoundingClientRect(),
        status:0, //0全部商品，1在卖商品；2草稿商品
        user:{},
        page:1,
        limit:10,
        last_page:1,
        endHide:true,
        list:[]
    },
    onLoad(options) {
        var user = wx.getStorageSync('userInfo')
        this.setData({user})
        this.getGoodList()
    },
    getGoodList() {
        request({
            url:"/miniprogram/user/getMyGoodList",
            data:{page:this.data.page,user_id:this.data.user.id,status:this.data.status}
        }).then(res=>{
            if(this.data.page == 1) this.setData({list:[]})
            this.setData({
                list:this.data.list.concat(res.data),
                last_page:res.last_page
            })
        })
    },
    edit(e){
        var good = this.data.list[e.currentTarget.dataset.index]
        switch (good.is_new){
            case 1:
                switch (good.type){
                    case 1:
                    case 2:
                    case 3:
                        wx.navigateTo({url:"/pages/add_juben_new/add_detail?id="+good.id})
                        break;
                    case 4:
                    case 5:
                        wx.navigateTo({url:"/pages/add_zhoubian/add_detail?id="+good.id})
                        break;
                }
                break;
            case 2:
                switch (good.type){
                    case 1:
                    case 2:
                    case 3:
                        wx.navigateTo({url:"/pages/add_juben_old/add_detail?id="+good.id})
                        break
                    case 4:
                    case 5:
                        wx.navigateTo({url:"/pages/add_zhoubian/add_detail?id="+good.id})
                        break;
                }
                break;
        }
        // 发布新剧盒装 pages/add_juben_new/add_detail？type=1
// 发布新剧城限 pages/add_juben_new/add_detail？ type=3
// 发布新剧城独 pages/add_juben_new/add_detail？type=2
//发布二手盒装 pages/add_juben_old/add_detail？type=1
//求购 pages/beg_good/add_detail
//发布周边新品 pages/add_zhoubian/add_detail？is_new=1
//发布周边二手 pages/add_zhoubian/add_detailis_new=
    },
    //切换上下架状态
    toggleStatus(e){
        var {id,status,index} = e.currentTarget.dataset
        var stxt = status ==  1 ? '上架' : '下架'
        wx.showModal({
            title:"真的要"+stxt+ '这件商品吗？',
            showCancel:true,
            success:rel=>{
                if(rel.confirm){
                    request({url:"/miniprogram/good/changeStatus",data:{id,status}}).then(res=>{
                        config.toast(res.msg,res.code,()=>{})
                        if(res.code == 200){
                            var list = this.data.list
                            list[index].status = status
                            this.setData({list})
                        }
                    })
                }
            }
        })

    },
    delGood(e){
        var {id,index} = e.currentTarget.dataset
        wx.showModal({
            title: "真的要删除这件商品吗？",
            showCancel: true,
            success: rel => {
                if(rel.confirm){
                    request({url:"/miniprogram/good/delGood",data:{id}}).then(res=>{
                        config.toast(res.msg,res.code,()=>{})
                        if(res.code == 200){
                            var list = this.data.list
                            list.splice(index,1)
                            // list[index].status = status
                            this.setData({list})
                        }
                    })
                }
            }
        })

    },
    //切换顶部筛选栏状态
    changeStatus(e){
        this.setData({
            status:e.currentTarget.dataset.status,
            page:1,
            last_page:1
        })
        this.getGoodList()
    },
    onReachBottom: function(e) {
        if(this.data.page >= this.data.last_page){
            this.setData({endHide:false})
            return false;
        }
        this.setData({page:this.data.page+1})
        this.getGoodList()
    },
})